<%@ page language="java" contentType="text/javascript; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Calendar"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.DecimalFormat"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>

<%
	response.setHeader("Access-Control-Allow-Origin", "*");
	response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String name = request.getParameter("name");
	String tingtitle = request.getParameter("tingtitle");
	String tingcontent = request.getParameter("tingcontent");
	//String tingtitle = new String(request.getParameter("tingtitle").getBytes("8859_1"), "utf-8");
	//String tingcontent = new String(request.getParameter("tingcontent").getBytes("8859_1"), "utf-8");
	String tingtheme = request.getParameter("tingtheme");
	String tingdate = request.getParameter("tingdate");
	String tingtime = request.getParameter("tingtime");
	String lat = request.getParameter("lat");
	String lng = request.getParameter("lng");
	//tingcontent = tingcontent.replace("\n","<br>"); // 줄바꿈 값을 <br>로 
	//tingcontent = tingcontent.replace("\u0020","&nbsp;"); // 띄어쓰기를 &nbsp;로
	String date = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
	String time = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date());

	Connection conn = null;
	PreparedStatement pstmt = null;
	JSONObject jo = new JSONObject();
	//DecimalFormat df = new DecimalFormat("00");
	//Calendar calendar = new Calendar.getInstance();
	
	//String year = Integer.toString(calendar.get(Calendar.YEAR));
	
	try{
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
		
		String insert = "insert into Ting" + "(ID, Host, Title, Content, Theme, Lat, Lng, StartDate, StartTime, Date, Time) " +
										"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
		pstmt = conn.prepareStatement(insert);
		pstmt.setString(1, id);
		pstmt.setString(2, name);
		pstmt.setString(3, tingtitle);
		pstmt.setString(4, tingcontent);
		pstmt.setString(5, tingtheme);
		pstmt.setString(6, lat);
		pstmt.setString(7, lng);
		pstmt.setString(8, tingdate);
		pstmt.setString(9, tingtime);
		pstmt.setString(10, date);
		pstmt.setString(11, time);
		pstmt.executeUpdate();
		
		jo.put("success", "true");
		out.print(jo);
	} catch(Exception e) {
		jo.put("success", "true");
		jo.put("1", "wrong");		
		jo.put("2", e);
		out.print(jo);
	}
	finally{
		try{
			pstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			conn.close();
		}
		catch (Exception ignored){
		}
	}
%>